/**
 * Created by yunpeng on 2015/3/26.
 */
angular.module('starter.orderServices', [])
    .factory('OrderService',function($http){

        var rows = 5;
        var datas = [
            {page:1
            ,status:2
            }
            ,{page:1
            ,status:3
            }
        ]
        var config = {
            timeout:8000
        }
        var order = [{total:undefined},{total:undefined}];
        var s = 0;
        var service = {
            switch:function(v ,scope){
                if(v === undefined){
                    return s;
                }else{
                    s = v;
                    if(!order[s].total ){
                        service.load({scope:scope})
                    }
                    setTimeout(function(){
                        scope.myScroll.refresh();
                    },300)
                }
            }
            ,busy:false
            ,orders:function(){
                return order;
            }
            ,index:[0,0]
            ,order:function(v){
                if(v === undefined){
                    return order[s].rows[service.index[s]]
                }else{
                    order[s].rows[service.index[s]] = v;
                }
            }
            ,load:function(params){
                if(service.busy){return}
                service.busy = true;
                var scroll = params.scope.myScroll;//刷新iScroll
                var param = {
                    page:1
                    ,rows: rows
                    ,status:datas[s].status
                    //,carrierid:params.carrierid
                }
                $http.post('/users/carrierorders' ,param ,config)
                    .success(function(data){
                        if(data.err){

                        }else{
                            if(data.total){
                                $('.noorder').hide();
                            }else{
                                $('.noorder').show();
                            }
                            truncateOrder(data.rows);
                            order[s].total = data.total;
                            order[s].rows = data.rows;
                            order.userid = data.userid;
                            datas[s].page = 1;
                        }
                        setTimeout(function(){
                            scroll.refresh();
                        },300)
                        service.busy = false;
                    })
                    .error(function(data){
                        setTimeout(function(){
                            scroll.refresh();
                        },300)
                        service.busy = false;
                    })
            }
            ,more:function(params){
                if(service.busy){return;}
                if(!order[s].total){
                    service.load(params);
                    return;
                }
                service.busy = true;
                var scroll = params.scope.myScroll;//刷新iScroll
                var param = {
                    page: datas[s].page + 1
                    ,rows: s.rows
                    ,status:datas[s].status
                    //,carrierid:params.carrierid
                }
                $http.post('/users/carrierorders' ,param ,config)
                    .success(function(data){
                        if(data.err){

                        }else{
                            if(data.rows.length == 0){
                                showToast('没有更多数据了');
                                //return;
                            }
                            truncateOrder(data.rows);
                            order[s].total = data.total;
                            order[s].rows = order[s].rows.concat(data.rows);
                            datas[s].page ++;
                        }
                        setTimeout(function(){
                            scroll.refresh();
                        },300)
                        service.busy = false;
                    })
                    .error(function(data){
                        setTimeout(function(){
                            scroll.refresh();
                        },300)
                        service.busy = false;
                    })
            }
            ,comment:null
        }

        return service;
    })
    .factory('BidService' ,function($http){
        var config = {
            timeout:8000
        }
        var s = {
            bids:{
                total:undefined
                ,rows:null
            }
            ,index:undefined
            ,page:1
            ,rows:5
            ,load:function(params){
                //var p = s.page;
                var param = {
                    page:1
                    ,rows: s.rows
                    //,carrierid:params.carrierid
                }
                $http.post('/users/mybid' ,param ,config)
                    .success(function(data){
                        if(data.err){

                        }else{
                            truncateOrder(data.rows);
                            s.bids.total = data.total;
                            s.bids.rows = data.rows;
                            s.bids.userid = data.userid;
                            s.page = 1;
                            setTimeout(function(){
                                params.scroll.refresh();
                            },300)
                        }
                    })
            }
            ,more:function(params){
                if(!s.bids.total){
                    s.load(params);
                    return;
                }
                var param = {
                    page: s.page + 1
                    ,rows: s.rows
                    //,carrierid:params.carrierid
                }
                $http.post('/users/mybid' ,param ,config)
                    .success(function(data){
                        if(data.err){

                        }else{
                            if(data.rows.length == 0){
                                showToast('没有更多数据了');
                                //return;
                            }
                            truncateOrder(data.rows);
                            s.bids.total = data.total;
                            s.bids.rows = s.bids.rows.concat(data.rows);
                            s.page ++;
                            setTimeout(function(){
                                params.scroll.refresh();
                            },300)
                        }
                    })
            }
        }
        return s;
    });

function truncateOrder(rows){
    var rs = rows;
    for(var x in rs){
        var i = rs[x];
        if(i.type == 1){
            var carnums = i.carnum.split(',');
            i.carnums = carnums;
            var nums = i.num.split(',');
            var totalnum = 0;
            for(var xx in nums){
                nums[xx] = Number(nums[xx]);
                totalnum += nums[xx];
            }
            i.nums = nums;
            i.total = totalnum;
            i.note = '本次共运送 '+carnums.length+'种型号共 '+totalnum+'辆车';
        }else if(i.type == 2){
            i.nums =  i.carnum.split(',');
            i.nums[0] = Number(i.nums[0])
            i.nums[1] = Number(i.nums[1])
            i.note = '本次共运送 ';
            i.cars = [];
            if(i.nums[0]){
                i.note += '小板'+i.nums[0] + '板 ';
                i.cars.push({
                    type:'小板'
                    ,num: i.nums[0]
                })
            }
            if(i.nums[1]){
                i.note += '大板'+i.nums[1] + '板';
                i.cars.push({
                    type:'大板'
                    ,num: i.nums[1]
                })
            }
        }
    }

}